package com.sxhuayuan.parking.controller.agent;

import java.util.List;

import javax.annotation.Resource;

import com.sxhuayuan.parking.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sxhuayuan.parking.compenent.Filter;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.entity.ParkChannel;
import com.sxhuayuan.parking.service.AgentService;
import com.sxhuayuan.parking.service.ParkChannelService;

/** 车场出口 */
@RestController("agentParkingLotExitController")
@RequestMapping("/agent/parkinglotexit")
public class ParkingLotExitController extends BaseController {

	@Autowired
	ParkChannelService service;

	@Resource(name = "agentServiceImpl")
	AgentService agentService;

	@RequestMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable) {

		Agent agent = agentService.getCurrent();
		if (agent == null) {
			return invalidToken();
		}

		List<Filter> filters = pageable.getFilters();
		Long childrenAgentId = null;
		if (filters != null) {
			for (Filter filter : filters) {
				if ("childrenAgentId".equals(filter.getProperty())) {
					childrenAgentId = ((Integer) filter.getValue()).longValue();
					break;
				}
			}
		}

		if (childrenAgentId != null) {
			Agent one = agentService.findOne(childrenAgentId);
			if (one == null) {
				return RespMessage.error("代理不存在");
			} else if (agentService.isPermission(agent, one)) {
				return successResp(service.findPage(pageable, ParkChannel.Type.exit, one));
			} else {
				return RespMessage.error("无权查看此代理");
			}
		} else {
			return successResp(service.findPage(pageable, ParkChannel.Type.exit, agent));
		}

	}

	/** 根据车场Id查询出口 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public RespMessage get(Long id) {
		ParkChannel carFieldExit = service.findOne(id);
		if (!carFieldExit.getParkingLot().getAgent().equals(agentService.getCurrent())) {
			return RespMessage.builder().error().setErr(Message.COMMON_NON_PERMISSION).build();
		}
		return RespMessage.builder().success().setDatas(carFieldExit).build();
	}

	/** 新增车场出口 */
	@RequestMapping("/save")
	public RespMessage save(@Validated @RequestBody ParkChannel exit) {
		service.save(exit);
		return RespMessage.builder().success().build();
	}

	@RequestMapping("/update")
	public RespMessage update(@Validated @RequestBody ParkChannel exit) {
		ParkChannel entity = service.findOne(exit.getId());
		if (agentService.isPermission(entity.getParkingLot().getAgent())) {
			return RespMessage.noPermissionError();
		}
		exit.setSn(entity.getSn());
		service.update(exit);
		return RespMessage.builder().success().build();
	}

	/** 删除 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public RespMessage delete(Long id) {
		ParkChannel entity = service.findOne(id);
		if (agentService.isPermission(entity.getParkingLot().getAgent())) {
			return RespMessage.noPermissionError();
		}
		service.delete(id);
		return RespMessage.builder().success().build();
	}
}
